/*this script is for cleaning webdatabase*/
var database = {};
function openHealthMagicBoxDatabase() {
    database = window.openDatabase("healthMagicBoxLocalDB", "0.1", "HealthMagicBox mobileDB", 2 * 1024 * 1024);
    console.log("openDatabase:" + database);
    return database;
};

function createUserTable() {

    var db = openHealthMagicBoxDatabase();

    db.transaction(function(tx) {
        tx.executeSql("CREATE TABLE IF NOT EXISTS Users(\
		id int unique ,\
		username varchar(20),\
		password varchar(50),\
		clearp varchar(20),\
		personID varchar(15),\
		role varchar(10), \
		email varchar(30),\
		phonenum varchar(15), \
		inviteID int NOT NULL,\
		thirdlogintype varchar(15),\
		thirdloginID varchar(15), \
		created datetime,\
		modified datetime,\
		active int)", [],
            function(tx, result) {
                console.log("users table is created.");
            },
            function(tx, error) {
                console.log("users table created failed" + error.message);
            });
    });
};

function createUserToPersonTable(db) {
   var db = openHealthMagicBoxDatabase();

  db.transaction(function(tx) {
      tx.executeSql("CREATE TABLE IF NOT EXISTS UserToPerson (\
	ID int INDENTITY PRIMARY KEY,\
	personID varchar(15),\
	userName varchar(30), \
	retitle varchar(10), \
	modified datetime)", [],
          function(tx, result) {
              console.log("UserToPerson table is created.");
          },
          function(tx, error) {
              console.log("UsetroPerson table created failed" + error.message);
          });
  });
};

function createPersonProfileTable(db) {
	var db = openHealthMagicBoxDatabase();

	db.transaction(function(tx) {
	 tx.executeSql("CREATE TABLE IF NOT EXISTS PersonProfile (\
			id int INDENTITY PRIMARY KEY  ,\
			personID varchar(50),\
			name varchar(50), \
			age int,\
			allergy varchar(50),\
			height double,	\
			weight double,\
			imageFileName varchar(50),\
			qq varchar(50),\
			sex varchar(50),\
			manualInput int,\
			sinaWeiBo varchar(50),\
			created datetime,\
			modified datetime,\
			active int,\
			life varchar(30),\
			eat varchar(30),\
			issues varchar(30),\
			birthDay int,\
			birthMonth int,\
			birthYear int,\
			invitedCode varchar(10),\
			sharelist varchar(100))", [],
	     function(tx, result) {
	         console.log("PersonProfiles table is created.");
	     },
	     function(tx, error) {
	         console.log("PersonProfiles table created failed" + error.message);
	     });
	});
};

var tableName = null;
function dropTable(name) {
    var db = openHealthMagicBoxDatabase();
    tableName = name;
    if (db != undefined) {
        db.transaction(function(tx) {
            tx.executeSql(
                "DROP TABLE " + tableName, [],
                function(tx, result) {
                    console.log("drop" + tableName + "successfully");
                },
                function(tx, error) {
                    console.log("drop" + tableName + "failed with " + error)
                });
        });
    }
}
